package dev.entao.core

import dev.entao.base.Mimes
import dev.entao.base.joinPath
import dev.entao.base.resourceBytes
import java.io.File

//不支持客户端文件缓存
//仅用于管理后台等访问很少的情况
class AssetController(context: HttpContext) : HttpController(context) {

    @MissAction
    fun onMissAction(subPaths: List<String>) {
        val s = subPaths.joinToString(File.separator)
        val path = joinPath(ASSET_DIR, s)
        val data = this::class.resourceBytes(path) ?: return context.sendError(404)
        //TODO 使用FileRender
        context.sendData(data, Mimes.ofFile(subPaths.last()))
    }

    companion object {
        var ASSET_DIR = "asset"
    }
}